package cn.holy.app.xcddt.repository

import androidx.lifecycle.LiveData
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.app.App.Companion.sharedPreferences
import cn.holy.app.xcddt.database.dao.UserDao
import cn.holy.app.xcddt.domain.LoginFormBean
import cn.holy.app.xcddt.domain.UserBean
import cn.holy.app.xcddt.domain.net.ApiResponse
import cn.holy.app.xcddt.net.ApiClient
import cn.holy.app.xcddt.net.callback.LoadingCallback
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import javax.inject.Inject


/**
 * Created by xionghao on 2018/2/22.
 */


class UserRepository @Inject constructor() {


    fun saveLoginInfo(username: String, password: String) {
        val sharedPreferences = App.sharedPreferences
        sharedPreferences.edit()
                .putString("username", username)
                .putString("password", password)
                .apply()
    }

    fun loadUser(): UserBean? {
        return App.userInfo
    }

    fun logoutUser() {
        App.userInfo = null
    }


    fun savePhone(phone: String, callback: LoadingCallback<UserBean>) {
        ApiClient.webService.savePhone(phone).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Subscriber<ApiResponse<UserBean>>() {
                    override fun onStart() {
                        super.onStart()
                        callback.onStart()
                    }

                    override fun onCompleted() {
                        callback.onCompleted()
                    }

                    override fun onError(e: Throwable) {
                        callback.onError(e.localizedMessage)
                    }

                    override fun onNext(userInfoBeanApiResponse: ApiResponse<UserBean>) {
                        if (userInfoBeanApiResponse.code == 200) {
                            val data = userInfoBeanApiResponse.data
                            App.userInfo = data
                            val token = userInfoBeanApiResponse.token
                            if (token != "") {
                                sharedPreferences.edit().putString("token", token)
                                        .apply()
//                                App.sharedPreferences.edit {
//                                    putString("token", token)
//                                }
                            }
                            callback.onSuccess(data!!)
                        } else {
                            callback.onError(userInfoBeanApiResponse.msg!!)
                        }
                    }
                })
    }


    fun loadLoginInfo(): LoginFormBean {
        val sharedPreferences = App.sharedPreferences
        val loginFormBean = LoginFormBean()
        loginFormBean.username = sharedPreferences.getString("username", "")
        loginFormBean.password = sharedPreferences.getString("password", "")
        return loginFormBean
    }

    fun doLogin(username: String, password: String, callback: LoadingCallback<UserBean>) {
//
        ApiClient.webService.login(username, password).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Subscriber<ApiResponse<UserBean>>() {
                    override fun onStart() {
                        super.onStart()
                        callback.onStart()
                    }

                    override fun onCompleted() {
                        callback.onCompleted()
                    }

                    override fun onError(e: Throwable) {
                        callback.onError(e.localizedMessage)
                    }

                    override fun onNext(userInfoBeanApiResponse: ApiResponse<UserBean>) {
                        if (userInfoBeanApiResponse.code == 200) {
                            val data = userInfoBeanApiResponse.data
                            App.userInfo = data
                            val token = userInfoBeanApiResponse.token
                            if (token != "") {
                                sharedPreferences.edit().putString("token", token)
                                        .apply()
                            }
                            callback.onSuccess(data!!)
                        } else {
                            callback.onError(userInfoBeanApiResponse.msg!!)
                        }
                    }
                })
    }

    fun refreshUser() {

    }
}
